import { reactive, ref, inject } from "vue";
import { useFlowStore } from '../../store/useFlowStore'

interface Tree {
    id: string;
    label: string;
    value: string;
}

export const useFlowTable = (currentPage: any, pageSize: any) => {
    //获取API配置
    const API = inject("API") as any;
    const ApiConfig = inject("ApiConfig") as any;
    const flowTable = reactive(
        {
            total: 0,
            tableData: [],
            queryCondition: {
                flowName: '',
                flowType: null
            },
            flowTree: ref<Tree[]>([])
        }
    );

    const flowStore = useFlowStore();

    //查询
    const queryData = async () => {
        try {
            const response = await API.request(
                ApiConfig.Methods.POST,
                ApiConfig.PATH.Flow.GetAllFlow,
                {
                    pageIndex: currentPage.value,
                    pageSize: pageSize.value,
                    condition: { flowName: flowTable.queryCondition.flowName },
                }
            );
            if (response.status == "200" && response.data.code == "200") {
                flowTable.tableData = response.data.data;
                flowTable.total = response.data.count;
            }
        } catch (error) {
            console.error(error);
        }
    };
    //新增
    const addData = async () => {
        try {
            const response = await API.request(
                ApiConfig.Methods.POST,
                ApiConfig.PATH.Flow.Create,
                flowStore.flowModel
            );
            if (response.status == "200" && response.data.code == "200") {
                return true;
            }
        } catch (error) {
            console.error(error);
        }
        return false;
    }
    //更新
    const updateData = async () => {
        try {
            const response = await API.request(
                ApiConfig.Methods.POST,
                ApiConfig.PATH.Flow.Update,
                flowStore.flowModel
            );
            if (response.status == "200" && response.data.code == "200") {
                return true;
            }
        } catch (error) {
            console.error(error);
        }
        return false;
    }
    //删除
    const deleteData = async (flowId) => {
        try {
            const response = await API.request(
                ApiConfig.Methods.GET,
                ApiConfig.PATH.Flow.DeleteByFlowId,
                { 'flowId': flowId }
            );
            if (response.status == "200" && response.data.code == "200") {
                return true;
            }
        } catch (error) {
            console.error(error);
        }
        return false;
    }
    //查询
    const getFlowByFlowType = async (flowTypeId) => {
        try {
            flowTable.flowTree = [];
            const response = await API.request(
                ApiConfig.Methods.POST,
                ApiConfig.PATH.Flow.GetAllFlow,
                {
                    pageIndex: 1,
                    pageSize: 100,
                    condition: { flowTypeId: flowTypeId },
                }
            );
            if (response.status == "200" && response.data.code == "200") {
                const transformMenuData: any = (FlowList: any[]) => {
                    return FlowList.map((f) => {
                        return {
                            id: f.id,
                            label: f.flowName,
                            value: f.flowName,
                        };
                    });
                };
                flowTable.flowTree = transformMenuData(response.data.data);
            }
        } catch (error) {
            console.error(error);
        }
    };

    return { flowTable, flowStore, queryData, addData, updateData, deleteData, getFlowByFlowType };
};